#include "qsvgbox.h"
#include "qprojectcore.h"
#include <QFileInfo>
#include <QPainter>
#include <QtSvg/QSvgRenderer>
#include "realtimedb.h"
#include "graphlets/graphletsutil.h"
QSvgBox::QSvgBox(QWidget *parent) : TagWidget(parent)
{
    m_szImageFile = "";
    m_bNoScale = false;
    m_iBoardWidth = 1;
    m_boardColorObj = Qt::black;
    this->update();
}
QSvgBox::~QSvgBox(){

}

QString QSvgBox::getImageFile()
{
    return m_szImageFile;
}

QString QSvgBox::getSvgCodeTemplate()
{
    return m_svgCodeTemplate;
}
void QSvgBox::setSvgCodeTemplate(const QString svgCode)
{
    if(!svgCode.isEmpty()){
        this->m_svgCodeTemplate = svgCode;
        qDebug() << "QSvgBox::setSvgCodeTemplate->var size 2" << m_tagMap.size();
    }
    qDebug() << "QSvgBox::setSvgCodeTemplate:" << m_svgCodeTemplate;
}

void QSvgBox::setImageFile(const QString szName)
{
    m_szImageFile = szName;
}

bool QSvgBox::isNoScale()
{
    return m_bNoScale;
}

void QSvgBox::setNoScale(bool bNoScale)
{
    m_bNoScale = bNoScale;
    this->update();
}

int QSvgBox::getBoardWidth()
{
    return m_iBoardWidth;
}

void QSvgBox::setBoardWidth(int iWidth)
{
    m_iBoardWidth = iWidth;
    this->update();
}

QColor QSvgBox::getBoardColor()
{
    return m_boardColorObj;
}

void QSvgBox::setBoardColor(QColor color)
{
    m_boardColorObj = color;
    this->update();
}

void QSvgBox::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
}

/**
 * 可能未初始化Properties
 * @brief QSvgBox::paintEvent
 * @param event
 */


void QSvgBox::paintEvent(QPaintEvent *event)
{
    updateSvgCode();
    //位置
    QRectF paintRect = this->property("geometry").toRectF();
    qDebug() << "QSvgBox::paintEvent:rect->" << paintRect;
    QPainter painter(this);
    painter.setRenderHints( QPainter::TextAntialiasing);
    QSvgRenderer svgRender(m_svgCodeGeneral.toUtf8(),this);
    svgRender.setFramesPerSecond(5);
    svgRender.setAnimationEnabled(true);
    svgRender.render(&painter);
    TagWidget::paintEvent(event);
    //update();
}

void QSvgBox::onTagChanged(VarTag tagVar){
    qDebug() << "QSvgBox::onTagChanged:tag name->" << tagVar.name;
    updateSvgCode();
    update();
}

void QSvgBox::onSettingChanged(QVariant oldValue,QVariant newValue,QString settingName){
    qDebug() << "QSvgBox::onSettingChanged:setting name->" << settingName << ",old value->" << oldValue << ",new value->" << newValue;
    foreach (QVariant var, m_tagMap.values()) {
        VarTag varTag = var.value<VarTag>();
        if(varTag.name==settingName){
            varTag.value = newValue.toString();
        }
        QVariant tempVar;
        tempVar.setValue(varTag);
        m_tagMap.remove(varTag.name);
        m_tagMap.insert(varTag.name,tempVar);
    }
    update();
}

void QSvgBox::updateTagValue(){

}

void QSvgBox::updateSvgCode(){
    m_svgCodeGeneral = m_svgCodeTemplate;
    qDebug() << "QSvgBox::updateSvgCode:tag size->" << m_tagMap.size();
    foreach (QVariant var, m_tagMap.values()) {
        VarTag varTag = var.value<VarTag>();
        qDebug() << "QSvgBox::updateSvgCode:tag Name->" << varTag.name;
        qDebug() << "QSvgBox::updateSvgCode:tag Value->" << varTag.value;
        qDebug() << "QSvgBox::updateSvgCode:tag Type->" << varTag.varType;
        QString varType = varTag.varType;
        if(varType == "Tag"){
            int vtagId = 0;
            vtagId = varTag.rtTagid.toInt();
            qDebug() << "QSvgBox::updateSvgCode:tag Id->" << vtagId;
            RunTimeTag *m_tag = RealTimeDB::instance()->tag(vtagId);
            if(m_tag){
                QString oldTagValue = varTag.value;
                QString tagValue = m_tag->toString();
                qDebug() << "QSvgBox::updateSvgCode:oldtagvalue->" << oldTagValue;
                qDebug() << "QSvgBox::updateSvgCode:tagvalue->" << tagValue;
                if(oldTagValue != tagValue){
                    varTag.value = tagValue;
                    m_tagMap.remove(varTag.name);
                    QVariant vTag ;
                    vTag.setValue(varTag);
                    m_tagMap.insert(varTag.name,vTag);
                    //qDebug() << "QSvgBox::updateSvgCode:svg code general final->" << m_svgCodeGeneral;
                    //m_svgCodeGeneral = m_svgCodeGeneral.replace("var("+varTag.name+")",varTag.value);
                    //qDebug() << "QSvgBox::updateSvgCode:svg code general final->" << m_svgCodeGeneral;
                    //qDebug() << "QSvgBox::updateSvgCode:svg code template final->" << m_svgCodeTemplate;

                }
                m_svgCodeGeneral = m_svgCodeGeneral.replace("var("+varTag.name+")",tagValue);
            }else{
                m_svgCodeGeneral = m_svgCodeGeneral.replace("var("+varTag.name+")",varTag.value);
            }
        }else{
            m_svgCodeGeneral = m_svgCodeGeneral.replace("var("+varTag.name+")",varTag.value);
        }
    }
    qDebug() << "QSvgBox::updateSvgCode:svg code general final->" << m_svgCodeGeneral;

}
